package com.sam.im.sms.util;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.database.Cursor;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.provider.ContactsContract;
import android.support.annotation.RequiresApi;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.RemoteViews;

import com.sam.im.sms.R;
import com.sam.im.sms.index.activitys.ThreadDetailActivity;
import com.sam.im.sms.reciever.NotificationBroadcastReceiver;

/**
 * 描述
 * liuyucheng
 * Created By 刘育成 on 2019/6/4.
 */
public class NotificationUtils extends ContextWrapper {
    private NotificationManager manager;
    public static final String id = "channel_1";
    public static final String name = "channel_name_1";
    private String[] contentProjection = new String[]{ContactsContract.PhoneLookup._ID,
            ContactsContract.PhoneLookup.DISPLAY_NAME};

    public NotificationUtils(Context context) {
        super(context);
    }

    public NotificationManager getManager() {
        if (manager == null) {
            manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        }
        return manager;
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    public void createNotificationChannel() {
        NotificationChannel channel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_HIGH);
        getManager().createNotificationChannel(channel);
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    public Notification.Builder getChannelNotification(Context context, String title, String content, String address) {
        //点击广播监听
        Intent intentClick = new Intent(this, NotificationBroadcastReceiver.class);
        intentClick.setAction("notification_clicked");
        intentClick.putExtra(NotificationBroadcastReceiver.TYPE, 1);
        intentClick.putExtra("phone", address);
        PendingIntent pendingIntentClick = PendingIntent.getBroadcast(this, 0, intentClick, PendingIntent.FLAG_ONE_SHOT);
        //cancle广播监听
        Intent intentCancel = new Intent(this, NotificationBroadcastReceiver.class);
        intentCancel.setAction("notification_cancelled");
        intentCancel.putExtra(NotificationBroadcastReceiver.TYPE, 1);
        PendingIntent pendingIntentCancel = PendingIntent.getBroadcast(this, 0, intentCancel, PendingIntent.FLAG_ONE_SHOT);
        RemoteViews headsUpView = new RemoteViews(context.getPackageName(), R.layout.custom_heads_up_layout);
        headsUpView.setTextViewText(R.id.tv_name, getName(address));
        headsUpView.setTextViewText(R.id.tv_content, content);
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        return new Notification.Builder(getApplicationContext(), id)
                //设置通知标题
                .setContentTitle(title)
                //设置通知内容
                .setContentText(content)
                //设置通知左侧的小图标
                .setSmallIcon(R.mipmap.ic_launcher)
                //设置点击通知后自动删除通知
                .setAutoCancel(true)
                //设置显示通知时间
                .setShowWhen(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntentClick)
                .setDeleteIntent(pendingIntentCancel)
                //设置自定义顶部提醒视图
                .setCustomHeadsUpContentView(headsUpView)
                .setDefaults(Notification.DEFAULT_ALL);
    }

    public NotificationCompat.Builder getNotification_25(Context context, String title, String content, String address) {
        //点击广播监听
        Intent intentClick = new Intent(this, NotificationBroadcastReceiver.class);
        intentClick.setAction("notification_clicked");
        intentClick.putExtra(NotificationBroadcastReceiver.TYPE, 1);
        intentClick.putExtra("phone", address);
        PendingIntent pendingIntentClick = PendingIntent.getBroadcast(this, 0, intentClick, PendingIntent.FLAG_ONE_SHOT);
        //cancle广播监听
        Intent intentCancel = new Intent(this, NotificationBroadcastReceiver.class);
        intentCancel.setAction("notification_cancelled");
        intentCancel.putExtra(NotificationBroadcastReceiver.TYPE, 1);
        PendingIntent pendingIntentCancel = PendingIntent.getBroadcast(this, 0, intentCancel, PendingIntent.FLAG_ONE_SHOT);
        RemoteViews headsUpView = new RemoteViews(context.getPackageName(), R.layout.custom_heads_up_layout);
        headsUpView.setTextViewText(R.id.tv_name, getName(address));
        headsUpView.setTextViewText(R.id.tv_content, content);

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        return new NotificationCompat.Builder(getApplicationContext())
                //设置通知标题
                .setContentTitle(title)
                //设置通知内容
                .setContentText(content)
                //设置通知左侧的小图标
                .setSmallIcon(R.mipmap.ic_launcher)
                //设置点击通知后自动删除通知
                .setAutoCancel(true)
                //设置显示通知时间
                .setShowWhen(true)
                //设置点击通知时的响应事件
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntentClick)
                .setDeleteIntent(pendingIntentCancel)
                //设置自定义顶部提醒视图
                .setCustomHeadsUpContentView(headsUpView)
                .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);


    }

    public void sendNotification(Context context, String title, String content, String address) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            createNotificationChannel();
            Notification notification = getChannelNotification(context, title, content, address).build();
            getManager().notify(1, notification);
        } else {
            Notification notification = getNotification_25(context, title, content, address).build();
            getManager().notify(1, notification);
        }
    }


    @RequiresApi(api = Build.VERSION_CODES.O)
    public Notification.Builder getSMS_O(String content) {
        return new Notification.Builder(getApplicationContext(), id)
                //设置通知内容
                .setContentText(content)
                //设置通知左侧的小图标
                .setSmallIcon(R.mipmap.ic_launcher)
                //设置点击通知后自动删除通知
                .setAutoCancel(true)
                //设置显示通知时间
                .setShowWhen(true)
                .setDefaults(Notification.DEFAULT_ALL);
    }

    public NotificationCompat.Builder getSMS_25(String content) {
        return new NotificationCompat.Builder(getApplicationContext())
                //设置通知内容
                .setContentText(content)
                //设置通知左侧的小图标
                .setSmallIcon(R.mipmap.ic_launcher)
                //设置点击通知后自动删除通知
                .setAutoCancel(true)
                //设置显示通知时间
                .setShowWhen(true)
                //设置自定义顶部提醒视图
                .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);
    }

    public void sendSMSNotification(String content) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            createNotificationChannel();
            Notification notification = getSMS_O(content).build();
            getManager().notify(1, notification);
        } else {
            Notification notification = getSMS_25(content).build();
            getManager().notify(1, notification);
        }
    }

    private String getName(String address) {
        /**
         * 定义成局部变量，不要定义成成员变量
         */
        String phoneName = null;
        Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
                Uri.encode(address));
        Cursor concatCursor = getContentResolver().query(uri,
                contentProjection, null, null, null);
        if (concatCursor.moveToFirst()) {
            // 查询到了联系人
            phoneName = concatCursor.getString(1);
        } else {
            phoneName = address;
        }
        return phoneName;
    }
}
